package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.TimeMarksAdapter;
import com.github.postsanf.yinian.bean.TimeMarks;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.SpUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.List;

public class TimeImpressActivity extends BaseActivity {
    private TextView creat_time;
    private ListView time_mark_list;
    private ImageView iv_back;
    private ImageView iv_null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_impress);
    }

    private long fristTime;

    // 友盟统计
    public void onResume() {
        super.onResume();
        registBroadcastReceiver();
        initData();
        initView();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        fristTime = System.currentTimeMillis();
    }

    private void registBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTIONs.TimeMarks);
        intentFilter.addAction(ACTIONs.ModifyTimeMarks);
        intentFilter.addAction(ACTIONs.DeleteTimeMarks);
        registerReceiver(MyReceiver, intentFilter);
    }

    BroadcastReceiver MyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.TimeMarks:
                case ACTIONs.ModifyTimeMarks:
                case ACTIONs.DeleteTimeMarks:
                    initData();
                    break;
            }
        }
    };

    private void initData() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(application.getCurGid()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowTimeMarks))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>() {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        TimeMarks timeMarks = gson.fromJson(s, TimeMarks.class);
                        if (timeMarks.getCode() == 0) {
                            List<TimeMarks.DataBean> data = timeMarks.getData();
                            if (data.size() == 0) {
                                iv_null.setVisibility(View.VISIBLE);
                            } else {
                                iv_null.setVisibility(View.GONE);
                            }
                            TimeMarksAdapter timeMarksAdapter = new TimeMarksAdapter(TimeImpressActivity.this, data);
                            time_mark_list.setAdapter(timeMarksAdapter);
                            SpUtils.put(TimeImpressActivity.this, "TimeImpressNoExist", false);
                        } else if (timeMarks.getCode() == 1012) {
                            SpUtils.put(TimeImpressActivity.this, "TimeImpressNoExist", true);
                            showToast(timeMarks.getMsg());
                            finish();
                        }
                        super.onSuccess(s, response);
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void initView() {
        creat_time = ((TextView) findViewById(R.id.creat_time));
        time_mark_list = ((ListView) findViewById(R.id.time_mark_list));
        iv_back = ((ImageView) findViewById(R.id.iv_back));
        iv_null = ((ImageView) findViewById(R.id.iv_null));
        iv_null.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        creat_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TimeImpressActivity.this, CreatTimeMarksActivity.class));
            }
        });
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(MyReceiver);
        super.onDestroy();
    }


    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
        Intent intent = new Intent(ACTIONs.record);
        intent.putExtra("key", "纪念日_3");
        intent.putExtra("value", new Long(System.currentTimeMillis() - fristTime).intValue());
        sendBroadcast(intent);
    }
}